# -*- coding: UTF-8 -*-

import re
import json


def load_json(path): # 读取json
    return json.load(open(path,'r', encoding='utf-8'))

def is_valid_data(data): # 判断是否无效数据，e.g. None、空数组、空字符串、无效字符串
    if not data:
        return False
    if type(data) == list:
        return len(data) > 0
    return len(data) > 0 and data.lower() != 'null'
    

def get_clean_data_from_url(raw): # 从url获得api名字
    temp = raw.split('/')
    temp.reverse()
    for i in temp:
        if i != '':
            return i.strip()
    return ''

remove_html_tag_pattern = re.compile('>(.*?)<')
def remove_html_tag(raw): # API的描述去除html标签
    return raw if not raw else ''.join(remove_html_tag_pattern.findall(raw)).strip()

def get_formats_from_string(raw): # 'a, b, c' -> [a,b,c]
    return [] if not is_valid_data(raw) else [fmt.lower() for fmt in [item.strip() for item in raw.split(',')] if fmt != '' and fmt != 'Unspecified']

if __name__ == '__main__':
    print(is_valid_data([]))
    print(is_valid_data([1,2]))
    print(is_valid_data(''))
    print(is_valid_data('hhh'))
    print(is_valid_data('Null'))

    print(get_clean_api_name("/api/bondora"))
    print(get_clean_api_name("/api/bondora/"))
    print(get_clean_api_name("a/b"))
    print(get_clean_api_name("zzz"))

    print(remove_html_tag('<div class=\"api_description tabs-header_description\"> The SemanticHacker API generates Semantic Signaturesï¿½. A Semantic Signatureï¿½ is a representation of ALL concepts covered in a block of text. Each block of text contains semantic dimensions with associated weights. The dimension weights capture the strength of each concept in the block of text.</div>'))
    print(remove_html_tag('<div class=\"api_description tabs-header_description\"> Mesos, a project of the Apache Software Foundation, shares the fundamental principles of Linux kernel. But Mesos kernel features additional functionalities such as unlimited compatibility with machines and provisioning of a variety of resource management and scheduling APIs. The Apache Mesos Operator HTTP API simplifies the construction of operator applications. The RESTful API makes it possible for developers to deploy HTTP requests as substitute for C++ or native client libraries when building operator applications. The HTTP requests facilitate interactions between master API calls and agent API calls. The API accepts strictly HTTP POST requests, usually encoded in JSON format, and returns responses in JSON format as well.</div>'))
